package cn.lwj.controller;


import cn.lwj.common.BaseContext;
import cn.lwj.common.R;
import cn.lwj.common.ServiceException;
import cn.lwj.dto.OrdersDto;
import cn.lwj.pojo.*;
import cn.lwj.service.*;
import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.javafx.scene.layout.region.BorderStrokeStyleSequenceConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.web.bind.annotation.*;

import javax.sound.sampled.Line;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.rmi.server.ServerCloneException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 分页查询订单数据
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, Integer number, String beginTime, String endTime){

        log.info("{},{},{},{},{}",page,pageSize,number,beginTime,endTime);

//        1.分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>();

//        2.条件查询
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Orders::getNumber,number);
//        queryWrapper.gt(endTime!=null,Orders::getOrderTime,beginTime);
//        queryWrapper.lt(beginTime!=null,Orders::getCheckoutTime,endTime);
//        3.分页查询
        ordersService.page(ordersPage,queryWrapper);

        return R.success(ordersPage);

    }



    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        ordersService.submit(orders);

        return R.success("创建订单成功");
    }


    @GetMapping("/userPage")
    public R<Page<OrdersDto>> getOrders(Integer page,Integer pageSize){

        Page<Orders> ordersPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,BaseContext.getCurrentId());
        lqw.orderByDesc(Orders::getOrderTime);

        ordersService.page(ordersPage,lqw);

//        创建ordersDto对象将完整信息响应给前端
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);

        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = records.stream().map((item) -> {

            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item, ordersDto);

            User user = userService.getById(BaseContext.getCurrentId());

//            查询默认收获地址
            LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
            addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, user.getId());
            addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault, 1);
            AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);

//            获取订单详情

            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(orderDetailLambdaQueryWrapper);

            ordersDto.setUserName(user.getName());

            ordersDto.setPhone(user.getPhone());

            if(addressBook != null){
                ordersDto.setAddress(addressBook.getDetail());
                ordersDto.setConsignee(addressBook.getConsignee());
            }

            if (orderDetails != null) {
                ordersDto.setOrderDetails(orderDetails);
            }

            return ordersDto;

        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);

        return R.success(ordersDtoPage);
    }


    @DeleteMapping("/delete")
    public R<String> delete(Long id){
        System.out.println( " id === " + id);
        ordersService.deleteOrder(id);
        return R.success("删除成功");
    }

    /**
     * 根据id修改状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        ordersService.updateStatus(orders);
        return R.success("修改成功");
    }

}
